﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BeeTest
{
    public abstract class WhenStateBase
    {
        private Exception       exceptionDuringWhen;

        public WhenStateBase(Exception exceptionDuringWhen)
        {
            this.exceptionDuringWhen = exceptionDuringWhen;
        }

        protected void AssertNoExceptionWasThrown()
        {
            if(this.exceptionDuringWhen != null) throw new BehaviorAssertionException(string.Format("An unexpected exception of type '{0}' was thrown.", this.exceptionDuringWhen.GetType().FullName), this.exceptionDuringWhen);
        }

        public void ThenThrow<TExc>() where TExc : Exception
        {
            var expectedType            = typeof(TExc);

            if(this.exceptionDuringWhen == null) 
            {
                throw new BehaviorAssertionException(string.Format("An exception of type '{0}' was excpeted, but none was thrown.", expectedType.FullName), this.exceptionDuringWhen);               
            }

            var thrownType              = this.exceptionDuringWhen.GetType();
            
            if(thrownType != expectedType)
            {
                throw new BehaviorAssertionException(string.Format("An exception of type '{0}' was expeced but the type of exception thrown was '{1}'.", expectedType.FullName, thrownType.FullName), this.exceptionDuringWhen);
            }
        }

        public void ThenDidntThrowException()
        {
            this.AssertNoExceptionWasThrown();
        }
    }
}
